package com.aixin.csopservice.repository;

import com.aixin.csopservice.domain.PointConTxnHist;
import com.aixin.csopuser.domain.CustomerDomain;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.Map;


@Repository
public interface PointRepository  extends JpaRepository<PointConTxnHist,Long> {

    @Query(value = "SELECT customerID FROM customer WHERE 1=1 " +
            "and (:certID is  null or certID = :certID)",nativeQuery = true)
    String getCustomerID(@Param("certID") String certID);


    @Query(value = "SELECT remnPoint FROM customer WHERE  customerID = :customerID",nativeQuery = true)
    Map<String,Integer>  getPoint(@Param("customerID") String customerID);

    @Query(value = "SELECT usedPoint FROM customer WHERE  customerID = :customerID",nativeQuery = true)
    Map<String,Integer> getUsedPoint(@Param("customerID") String customerID);

    @Modifying
    @Query(value = "UPDATE pointcontxnhist SET pointStatus = 2 " +
            " WHERE orderNumber =:orderNumber",nativeQuery = true)
    int chargeback(@Param("orderNumber") String orderNumber);

    @Query(value = "SELECT txnPoint from pointcontxnhist where orderNumber = :orderNumber",nativeQuery = true)
    int getTxnPoint(@Param("orderNumber") String orderNumber);

    @Modifying
    @Query(value = "UPDATE customer SET remnPoint = :remnPoint ,usedPoint= :usedPoint" +
            " WHERE customerID = :customerID",nativeQuery = true)
    int updateCustPoint(@Param("remnPoint") int remnPoint,@Param("usedPoint") int usedPoint, @Param("customerID") String customerID);

    @Query(value = "select  pointGenNumber from pointinctxnhist where customerID=:customerID order by expireDate desc LIMIT 1",nativeQuery = true)
    String getIncGenNumber(@Param("customerID") String customerID);


}
